'use strict';
app.service('promotionPosterAddService', ['utilService', '$q', '$http', function (utilService, $q, $http) {
    var _this_ = this;
    _this_.query=function($scope){
        var deferred=$q.defer();
        utilService.get({
            url: $scope.app.domain + "promotion/poster/id",
            params:$scope.promotionPosterVO
        }).then(function(rlt){
            deferred.resolve(rlt.data);
        });
        return deferred.promise;
    }

    _this_.waterMark=function($scope){
        var deferred=$q.defer();
        utilService.api({
            url: $scope.app.domain + "water/mark/add",
            data: $scope.promotionPosterVO
        }).then(function(rlt){
            deferred.resolve(rlt);
        });
        return deferred.promise;
    }
    _this_.updateData = function ($scope) {
        var deferred = $q.defer();
        utilService.api({
            url: $scope.app.domain + "/promotion/poster/set",
            data: $scope.promotionPosterVO
        }).then(function (rlt) {
            deferred.resolve(rlt);
        });
        return deferred.promise;
    }

    _this_.download = function ($scope) {
        $http({
            method:'post',
            url:$scope.app.domain + 'water/mark/add',
            data:$scope.param,
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function (data) {
            var blob = new Blob([data], {type: "image/png"});
            var fileName = "promotion_poster" + (new Date()).getTime() + '.png';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        });

        _this_.findById=function(id){
            var deferred=$q.defer();
            utilService.get({
                url:$scope.app.domain + 'promotion/poster/id',
                params:{id:id}
            }).then(function(response){
                deferred.resolve(response.data);
            },function(e){
                console.error(e);
                deferred.reject("连接服务器出错！");
            });
            return deferred.promise;
        }
    }
}]);